// st.h: interface for the st class.
//
//////////////////////////////////////////////////////////////////////

#ifndef AFX_ST_H__A24ECDE5_923A_40A9_8391_9C89A2447BC5__INCLUDED_
#define AFX_ST_H__A24ECDE5_923A_40A9_8391_9C89A2447BC5__INCLUDED_


template<class T,int MAX_NUM>
class SArray
{
public:
	SArray(){Clear();};
	int Add(T& item);
	bool Add(int id, T& item);
	void Delete(int id);
	int Find(T& item);
	void Clear(){num=0;last=-1;};


	int Count(){return num;}
	int Last(){	return last;}
	bool IsSet(int id){if(id>=0 && id<=last)return isset[id];return false;}


	T& operator [] (int id){return arr[id];}


private:
	T arr[MAX_NUM];
	bool isset[MAX_NUM];
	int num,last;

};

template<class T,int MAX_NUM>
int SArray<T,MAX_NUM>::Add(T& item)
{
	int id;
	if(num==MAX_NUM)return -1;
	if(num==last+1)
	{
		last++;
		id = last;
	}else
	{
		bool *is=isset;
		for(id=0;id<MAX_NUM;id++,is++)
			if(!*is)break;

	}
	isset[id]=true;
	arr[id] = item;
	num++;
	return id;
}

template<class T,int MAX_NUM>
bool SArray<T,MAX_NUM>::Add(int id, T& item)
{
	if(id<0 || id>=MAX_NUM) return false;
	if(isset[id])return false;
	if(id>last)last = id;
	isset[id]=true;
	arr[id] = item;
	num++;
	return true;
}

template<class T,int MAX_NUM>
void SArray<T,MAX_NUM>::Delete(int id)
{
	if(isset[id])
	{
		isset[id]=false;
		num--;
		if(last==num)last--;
	}
}
template<class T,int MAX_NUM>
int SArray<T,MAX_NUM>::Find(T& item)
{
	bool *is=isset;
	T* it=arr;
	for(int i=0;i<=last;i++,is++,it++)
		if(*is)
			if(*it == item)
				return i;
		
	return -1;
}

#endif



